from Devices.Chiot.BsDevs.ComDev.ComDevParser import ComDevParser
from Devices.Chiot.BsDevs.Plug.PlugDevice import PlugDevice

class PlugParser(ComDevParser):
    def ParseCom(msgvalue, fullStatus):
        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        ret, value = PlugParser.GetIntProperty(stateValue, "switch", "on")
        if ret:
            fullStatus.switchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "memSwitch", "on")
        if ret:
            fullStatus.memSwitchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "backligh", "on")
        if ret:
            fullStatus.backlighStatus = value
            
        ret, value = PlugParser.GetIntProperty(stateValue, "chargingPro", "on")
        if ret:
            fullStatus.chargingProStatus = value
        return True

    def ParseHeartbeat(msgValue, fullStatus):


        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        ret, value = PlugParser.GetIntProperty(stateValue, "switch", "on")
        if ret:
            fullStatus.switchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "memSwitch", "on")
        if ret:
            fullStatus.memSwitchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "backligh", "on")
        if ret:
            fullStatus.backlighStatus = value
            
        ret, value = PlugParser.GetIntProperty(stateValue, "chargingPro", "on")
        if ret:
            fullStatus.chargingProStatus = value
        
        PlugParser.GetTimers(stateValue, "timer", fullStatus.timers, PlugDevice.Timer())
        PlugParser.GetDelay(stateValue, "delay", fullStatus.delay)
        return True

    def ParseRptStatus(msgValue, fullStatus):
        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        ret, value = PlugParser.GetIntProperty(stateValue, "switch", "on")
        if ret:
            fullStatus.switchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "memorySwitch", "on")
        if ret:
            fullStatus.memSwitchStatus = value

        ret, value = PlugParser.GetIntProperty(stateValue, "backlight", "on")
        if ret:
            fullStatus.backlightStatus = value
            
        ret, value = PlugParser.GetIntProperty(stateValue, "chargingPro", "on")
        if ret:
            fullStatus.chargingProStatus = value
        return True

    def ParseHomekitInfo(msgValue, fullStatus):
        parse_ret = 0
        ret, strValue = ComDevParser.GetStrProperty(msgValue, "setupCode")
        if ret:
            fullStatus.setupCode = strValue
            parse_ret += 1

        ret, strValue = ComDevParser.GetStrProperty(msgValue, "setupPayload")
        if ret:
            fullStatus.setupPayload = strValue
            parse_ret += 1

        ret, strValue = ComDevParser.GetStrProperty(msgValue, "hotPlug")
        if ret:
            fullStatus.hotPlug = int(strValue)
            parse_ret += 1
        
        return True if parse_ret else False